﻿using Common;
using DotNetCore.CAP;
using DB.Model;
using DB.MongoDB;
using DB.MongoDB.Model;
using Http;
using NoticeCorpWechat.interfaces;
using NoticeCorpWechat.postdata;
using NoticeCorpWechat.result;
using NoticeWechat.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NoticeWechat.Hangfire
{
    public class MessageJob
    {
        IMongoDbHelper<Tasks> _Db;
        IMongoDbHelper<User> _DbUser;
        ICapPublisher _capPublisher;
        IHttpClientHelper _IHttpClientHelper;
        public MessageJob(IMongoDbHelper<Tasks> mongoDb, IMongoDbHelper<User> DbUser, IHttpClientHelper IHttpClientHelper, ICapPublisher capPublisher)
        {
            _IHttpClientHelper = IHttpClientHelper;
            _Db = mongoDb;
            _capPublisher = capPublisher;
            _DbUser = DbUser;
        }
        public void RecurringJob_Reset_Status()
        {
            //重置所有循环任务 状态为0
            FilterGroup where = new();
            where.FilterRules.Add(new FilterRule("Status", "1"));
            where.FilterRules.Add(new FilterRule("TaskType", "1"));
            Dictionary<string, string> setValues = new();
            setValues.Add("Status", "0");
            _Db.UpdateManyAsync(where, setValues);

            //删除执行一次的 已执行的
            where = new();
            where.FilterRules.Add(new FilterRule("Status", "1"));
            where.FilterRules.Add(new FilterRule("TaskType", "0"));
            _Db.Delete(where);

            //还原每日发信息数量
            var pp = new FilterGroup();
            pp.FilterRules.Add(new FilterRule("State", "1"));
            var ss = new Dictionary<string, string>();
            ss.Add("UseTimes", "0");
            _DbUser.UpdateManyAsync(pp, ss);
        }
        public void SendMessage()
        {
            var NowWeekDay = 0;
            switch (DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    NowWeekDay = 1;
                    break;
                case DayOfWeek.Tuesday:
                    NowWeekDay = 2;
                    break;
                case DayOfWeek.Wednesday:
                    NowWeekDay = 3;
                    break;
                case DayOfWeek.Thursday:
                    NowWeekDay = 4;
                    break;
                case DayOfWeek.Friday:
                    NowWeekDay = 5;
                    break;
                case DayOfWeek.Saturday:
                    NowWeekDay = 6;
                    break;
                case DayOfWeek.Sunday:
                    NowWeekDay = 7;
                    break;
            }

            List<Tasks> tasks = new List<Tasks>();
            if (_Db.AsQueryable().Where(x => x.Status == 0).ToList().IsNullOrEmpty()) return;
            //循环任务
            tasks.AddRange(_Db.AsQueryable().Where(x => x.Status == 0 &&
            x.TaskType == 1 &&
            (x.TaskRunType == 0 || x.TaskRunType == NowWeekDay) &&
            x.StartMinute <= DateTime.Now.Minute
            && x.StartHour <= DateTime.Now.Hour && x.FirstDate <= DateTime.Now
            ).ToList());

            //触发一次
            tasks.AddRange(_Db.AsQueryable().Where(x => x.Status == 0 &&
           x.TaskType == 0 && x.FirstDate <= DateTime.Now
           ).ToList());

            if (tasks.IsNullOrEmpty()) return;
            foreach (var i in tasks)
            {
                if (i.TaskStartType == 0)
                {
                    _capPublisher.PublishAsync("WeChatTextMessage", new string[] { i.Id.ToString() });
                }
                else
                {
                    switch (i.TaskName)
                    {
                        case "天气预报":
                            //i.Content == city ; 没提交, 注释就好
                             new WeatherJob(_IHttpClientHelper, _capPublisher).SearchWeather(i.Id.ToString(), i.Content);
                            break;
                    }
                }
            }
        }

    }
}
